using System;
using System.IO;
using System.Diagnostics;
using EnvDTE;

namespace NDocBuilder
{
	/// <summary>
	/// Class that will launch the ndoc compilation
	/// </summary>
	public class NDocProcess
	{
		/// <summary>
		/// Constructor for the NDocProcess class
		/// </summary>
		/// <param name="strNDocProjectFilename">The NDoc project file to compile</param>
		public NDocProcess(string strNDocProjectFilename)
		{
			m_strNDocProjectFilename = strNDocProjectFilename;
		}

		#region Properties

		/// <summary>
		/// The pane we use for text output
		/// </summary>
		public OutputWindowPane OutputPane
		{
			get{return Builder.Current.OutputPane;}
		}

		/// <summary>
		/// The ndoc executable that compiles the documentation
		/// </summary>
		public string NDocExecutable
		{
#if DEBUG
			get{return @"C:\Program Files\NDoc 1.3\bin\net\1.1\NDocConsole.exe";}
#else
			get
			{
				string strExecFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
				return Path.Combine(strExecFolder, "NDocConsole.exe");
			}
#endif
		}

		private string m_strNDocProjectFilename;
		/// <summary>
		/// The ndoc project file
		/// </summary>
		public string NDocProjectFilename
		{
			get{return m_strNDocProjectFilename;}
		}

		/// <summary>
		/// The ndoc project folder
		/// </summary>
		public string NDocProjectFolder
		{
			get{return Path.GetDirectoryName(NDocProjectFilename);}			
		}

		#endregion

		/// <summary>
		/// Spawns the compiler process
		/// </summary>
		public void Exec()
		{
			OutputPane.OutputString("\nCompiling documentation ...\n");
			OutputPane.OutputString("--------------------------------------------------------------------------------\n");

			ProcessStartInfo ps			= new ProcessStartInfo(NDocExecutable, "-project=" + Path.GetFileName(NDocProjectFilename)); 
			
			ps.WorkingDirectory			= NDocProjectFolder;
			ps.UseShellExecute			= false;
			ps.RedirectStandardOutput	= true;
			ps.CreateNoWindow			= true;

			System.Diagnostics.Process p = System.Diagnostics.Process.Start(ps);


			while(!p.HasExited)
			{
				OutputPane.OutputString(p.StandardOutput.ReadLine() + "\n");
				//System.Threading.Thread.Sleep(100);
			}

			OutputPane.OutputString(p.StandardOutput.ReadToEnd() + "\n");
			OutputPane.OutputString("--------------------------------------------------------------------------------\n");
		}
	}

	
	
}
